package tech.horgee.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;
import tech.horgee.common.IWrapper;
import tech.horgee.group.InsertGroup;
import tech.horgee.group.UpdateGroup;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Objects;
import java.util.Set;

/**
 * 角色信息
 *
 * @author haojun
 * @since 2022-06-12
 */
@Getter
@Setter
@TableName("tab_role")
public class Role implements Serializable, IWrapper<Role> {

    private static final long serialVersionUID = 1L;

    /**
     * 主键
     */
    @NotNull(groups = UpdateGroup.class)
    @TableId(value = "role_id", type = IdType.AUTO)
    private Long roleId;

    /**
     * 角色名称
     */
    @NotBlank(groups = InsertGroup.class)
    private String roleName;

    /**
     * 显示顺序
     */
    private Integer roleSort;

    /**
     * 乐观锁
     */
    @NotNull(groups = UpdateGroup.class)
    @Version
    private Long version;

    /**
     * 逻辑删除
     */
    @TableLogic
    private Boolean deleted;

    /**
     * 创建时间
     */
    @TableField(updateStrategy = FieldStrategy.NEVER)
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    @TableField(updateStrategy = FieldStrategy.NEVER)
    private LocalDateTime updateTime;

    /**
     * 权限集合
     */
    @TableField(exist = false)
    private Set<String> permissions;

    /**
     * 权限ID集合
     */
    @TableField(exist = false)
    private Set<Long> menuIds;

    @Override
    public LambdaQueryWrapper<Role> wrapper() {
        return new LambdaQueryWrapper<Role>()
                .eq(Objects.nonNull(roleId), Role::getRoleId, roleId)
                .like(Objects.nonNull(roleName), Role::getRoleName, roleName)
                .eq(Objects.nonNull(roleSort), Role::getRoleSort, roleSort)
                .eq(Objects.nonNull(version), Role::getVersion, version)
                .eq(Objects.nonNull(deleted), Role::getDeleted, deleted)
                .eq(Objects.nonNull(createTime), Role::getCreateTime, createTime)
                .eq(Objects.nonNull(updateTime), Role::getUpdateTime, updateTime)
                .orderByDesc(Role::getRoleSort)
                ;
    }
}
